package steg.img;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import bitmap.BMPSave;
import bitmap.BMPobj;
import bitmap.Color;

public class StegOnImg extends StegNewImg
{

	private String imgpath;

	public StegOnImg(String path, String imgpath)
	{
		super(path);
		this.imgpath = imgpath;
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException
	{
		System.out.println("*** Start ***");
		String path = "20130614";
		//path = "C:/tmp/test/divina";
		String imgPath = "C:/tmp/test/divina/test500Grey.bmp";
	//	imgPath = "C:/tmp/test/3x3/imgMapTest_Steg" + ".bmp";
		StegOnImg stegOnImg = new StegOnImg(path, imgPath);
		// StegNewImg stegNewImg = new StegNewImg("C:/WorkSpaceDM_Diego/");
		stegOnImg.process();
		System.out.println("*** end ***");

	}

	private void process() throws IOException
	{
		ArrayList<String> listFile = retriveList(path + "/");
		System.out.println("Found : " + listFile.size() + " Files");
		StringBuilder blob = blobizeIt(listFile);
		System.out.println("chars : " + blob.length());
		//int[][] img = getMapImg(imgpath);
		//StegOnImgProcess(img, blob);

	}

	private void StegOnImgProcess(int[][] img, StringBuilder blob) throws IOException
	{
		//blob=new StringBuilder("ABC123xyz");
		int rows=img.length;
		
		int[][] imgSteg = img;
		
		for (int i = 0; i < blob.length(); i++)
		{
			int r =i/rows;
			int c=i%rows;
			
			if (r>=rows || c>=rows){
				System.out.println("img too small");
				continue;
				//return;
			}

			char ch = blob.charAt(i);
			int newcolor = stegSinglePoint(ch,img[r][c]);
			imgSteg[r][c]=newcolor;
		}
		
		BMPobj imgS = new BMPobj(imgSteg);
		BMPSave bmp = new BMPSave();
		String path = imgpath+"_Steg" + ".bmp";
		bmp.saveBMP(path, imgS.image);

	}

	private int stegSinglePoint(char ch, int i)
	{
		Color c = new Color(i);
		int r=c.getRed()/10;
		int g=c.getGreen()/10;
		int b=c.getBlue()/10;
//		System.out.println("color: "+ r+" "+g+" "+b);
		int c000=ch/100;
		int c00 = (ch-c000*100)/10;
		int c0=(ch-c000*100-c00*10);
		int nr=r*10+c000;
		if(nr>255)
			nr=nr-10;
		
		int ng=g*10+c00;
		if(ng>255)
			ng=ng-10;
		
		int nb=b*10+c0;
		if(nb>255)
			nb=nb-10;
		Color nc = new Color(nr,ng,nb);
//		System.out.println("original:"+c+"\tnew:"+nc+"-->"+(ch*1));
//		System.out.println("original: \t"+c);
//		System.out.println("chhar: \t"+ c000+" "+c00+" "+c0);
//		System.out.println("stegan:\t\t"+nc);
		
		return nc.getRgbInt();
	}

	private int[][] getMapImg(String imgPath) throws IOException
	{

		File ff=new File(imgPath);
		System.out.println(ff.exists());
		// BufferedImage image = ImageIO.read(new
		// File("c:/tmp/steg1372421970184.bmp"));
		BufferedImage image = ImageIO.read(ff);

		int row = image.getHeight();
		int col = image.getWidth();
		int[][] imgMap = new int[row][col];
		for (int r = 0; r < row; r++)
		{
			for (int c = 0; c < col; c++)
			{
				Color ccc = new Color(image.getRGB(c, row - r - 1) + 16777216);
				imgMap[r][c] = new Color(ccc.getBlue(), ccc.getGreen(), ccc.getRed()).getRgbInt();
				//System.out.println(ccc);
			}
		}

//		BMPobj img = new BMPobj(imgMap);
//		BMPSave bmp = new BMPSave();
//		String path = "c:/tmp/imgMapTest_" + ".bmp";
//		bmp.saveBMP(path, img.image);

		return imgMap;
	}

}
